package com.wj.book.interceptor;

import com.wj.book.constants.Constants;
import com.wj.book.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.transaction.managed.ManagedTransaction;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import sun.security.krb5.internal.APRep;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Component
public class LoginInterception implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response ,
                             Object handler){
        log.info("目标方法执行前执行:LoginInterception.preHandle");
        HttpSession session = request.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if(userInfo==null||
           userInfo.getId()<1){
            response.setStatus(401);
            return false;
        }
        return true;
    }

}